﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rename.Util
{
    public partial class FileHelper
    {
        private const string DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        #region 获取文件大小并以B，KB，GB，TB
        /// <summary>
        /// 计算文件大小函数(保留两位小数),Size为字节大小
        /// </summary>
        /// <param name="size">初始文件大小</param>
        /// <returns></returns>
        public static string ToFileSize(string fileName,out string crTime,out string mTime)
        {
            string m_strSize = "";
            long FactSize = 0;
            FileInfo fileInfo = new FileInfo(fileName);
            FactSize = fileInfo.Length;
            crTime=fileInfo.CreationTime.ToString(DATE_FORMAT); 
            mTime=fileInfo.LastWriteTime.ToString(DATE_FORMAT);

            if (FactSize < 1024.00)
                m_strSize = FactSize.ToString("F2") + " B";
            else if (FactSize >= 1024.00 && FactSize < 1048576)
                m_strSize = (FactSize / 1024.00).ToString("F2") + " KB";
            else if (FactSize >= 1048576 && FactSize < 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " MB";
            else if (FactSize >= 1073741824)
                m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " GB";
            return m_strSize;
        }
        #endregion

        #region 获取指定目录及子目录中所有文件列表
        /// <summary>
        /// 获取指定目录及子目录中所有文件列表
        /// </summary>
        /// <param name="directoryPath">指定目录的绝对路径</param>
        /// <param name="searchPattern">模式字符串，"*"代表0或N个字符，"?"代表1个字符。
        /// 范例："Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
        /// <param name="isSearchChild">是否搜索子目录</param>
        public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
        {
            //如果目录不存在，则抛出异常
            if (!Directory.Exists(directoryPath))
            {
                throw new FileNotFoundException();
            }
            try
            {
                if (isSearchChild)
                {
                    return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
                }
                else
                {
                    return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }
        #endregion

        public static string IsSpecialChar(string fileName)
        {
            StringBuilder sb=new StringBuilder();
            char[] notInFileChar = new char[] { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
            foreach (char item in fileName) 
            {
                if (notInFileChar.Contains(item) == true)
                {
                    sb.Append(item + ",");
                }
            }
            return sb.ToString().TrimEnd(',');
        }
    }
}
